const parallelLimit = require('run-parallel-limit')
const upload_single = require("./upload-single")


/**
 * 
 * @param {String[]} full_paths 
 * @param {Number} max_uploads 
 */
function upload(full_paths, max_uploads) {
    let paths = Array.from(full_paths);
    return new Promise((resolve) => {
        let tasks = paths.map(full_path => {
            return cb => {
                upload_single.uploadSingleForXTime(3, full_path).then(() => cb(null, full_path + " ok!")).catch(error => {
                    cb({ full_path, error });
                })
            }
        });

        parallelLimit(tasks, max_uploads, function (err, results) {
            console.log("\n\n parallel tasks done..");
            if (err) {
                console.log(err);
            }
            if (results) {
                console.log(results);
            }
        })
    })

}

module.exports = {
    upload:upload
}